<?php

class ProgramsController extends AppController {

    var $name = 'Programs';
    var $uses = array('User', 'FocusArea', 'UserArea', 'Program','FocusProgram','Will');

    function beforeFilter() {
        parent::beforeFilter();
        // $this->Auth->autoRedirect = false;
        //$this->Auth->allowedActions = array('success', 'add', 'emailcheck', 'usercheck', 'login', 'genRandomString', 'forget_password', 'backstage_login', 'logout');
        // $this->Auth->allowedActions = array('*');
    }

    function index() {
        $this->Program->recursive = 0;
        $this->set('programs', $this->paginate());
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid program', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('program', $this->Program->read(null, $id));
    }

    function create() {
        //$this->layout = false;
        if (!empty($this->data)) {                     
         // pr($this->data);
            $this->Program->create();  
            //unset($this->Program->Will->validate['program_id']);
            if ($this->Program->saveAll($this->data, array('validate'=>'first'))) {
                $program_id = $this->Program->getLastInsertId();
                $i = 0;

                foreach ($this->data['Program']['focus_area_id'] as $focus_id) {
                    $this->data['FocusProgram'][$i]['focus_area_id'] = $focus_id;
                    $this->data['FocusProgram'][$i]['program_id'] = $program_id;
                    $i++;
                }

                $this->FocusProgram->create();
                $this->FocusProgram->saveAll($this->data['FocusProgram']);
                $this->Session->setFlash(__('The program has been saved', true));                
            } else { 
                //pr($this->Program->invalidFields());
                //pr($this->Will->invalidFields());
                
                $this->Session->setFlash(__('The program could not be saved. Please, try again.', true));
            }
        }
        $this->set('focus_areas', $this->FocusArea->find('list'));
        //$users = $this->Program->User->find('list');
        //$this->set(compact('users'));
    }

    function edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid program', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Program->save($this->data)) {
                $this->Session->setFlash(__('The program has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The program could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Program->read(null, $id);
        }
        $users = $this->Program->User->find('list');
        $this->set(compact('users'));
    }

    function delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for program', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Program->delete($id)) {
            $this->Session->setFlash(__('Program deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Program was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

    function backstage_index() {
        $this->Program->recursive = 0;
        $this->set('programs', $this->paginate());
    }

    function backstage_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid program', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('program', $this->Program->read(null, $id));
    }

    function backstage_add() {
        if (!empty($this->data)) {
            $this->Program->create();
            if ($this->Program->save($this->data)) {
                $this->Session->setFlash(__('The program has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The program could not be saved. Please, try again.', true));
            }
        }
        $users = $this->Program->User->find('list');
        $this->set(compact('users'));
    }

    function backstage_edit($id = null) {
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid program', true));
            $this->redirect(array('action' => 'index'));
        }
        if (!empty($this->data)) {
            if ($this->Program->save($this->data)) {
                $this->Session->setFlash(__('The program has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The program could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Program->read(null, $id);
        }
        $users = $this->Program->User->find('list');
        $this->set(compact('users'));
    }

    function backstage_delete($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for program', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Program->delete($id)) {
            $this->Session->setFlash(__('Program deleted', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Program was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}